<!--
 * @Author: Rain
 * @Date: 2023/09/26 14:43:34
 * @LastEditTime: 2023/11/10 23:22:33
 * @Description: Do Something
-->
<template>
  <div class="app-container w-full h-full overflow-hidden">
    <div class="head-container w-full h-full overflow-hidden flex flex-col">
      <div class="flex items-center justify-end w-full search pb-10">
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.typeName"
          clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
        <crudOperation :permission="permission" />
      </div>
      <div class="w-full flex-1 overflow-hidden">
        <el-table
          ref="table"
          v-loading="crud.loading"
          :data="crud.data"
          size="small"
          style="width: 100%"
          height="100%"
          @selection-change="crud.selectionChangeHandler"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="typeName" label="名称" />
          <el-table-column prop="typeStatus" label="状态">
            <template slot-scope="scope">
              {{ dict.label.discount_use[scope.row.typeStatus] }}
            </template>
          </el-table-column>
          <el-table-column prop="showIndex" label="首页展示">
            <template slot-scope="{ row }">
              {{ row.showIndex ? "是" : "否" }}
            </template>
          </el-table-column>
          <el-table-column prop="typeIcon" label="图标">
            <template slot-scope="{ row }">
              <img
                :src="row.typeIcon | imgTransform"
                alt=""
                style="width: 40px; height: 40px"
              >
            </template>
          </el-table-column>
          <el-table-column
            v-if="checkPer(['admin', 'courseType:edit', 'courseType:del'])"
            label="操作"
            width="150px"
            align="center"
          >
            <template slot-scope="scope">
              <udOperation :data="scope.row" :permission="permission" />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination />
    </div>
    <el-dialog
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="500px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="名称" prop="typeName">
          <el-input v-model="form.typeName" style="width: 370px" />
        </el-form-item>
        <el-form-item label="状态" prop="typeStatus">
          <el-select
            v-model="form.typeStatus"
            filterable
            placeholder="请选择"
            class="w-full"
          >
            <el-option
              v-for="item in dict.discount_use"
              :key="item.id"
              :label="item.label"
              :value="item.value * 1"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="首页展示" prop="showIndex">
          <el-radio-group v-model="form.showIndex">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="图标" prop="typeIcon">
          <uploadImage ref="uploadImage" :src="form.typeIcon" @upload="uploadAction" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="default" @click="crud.cancelCU">取消</el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          @click="doSubmitAction"
        >确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudCourseType from '@/api/courseType'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import uploadImage from '@/components/uploadImage'
const defaultForm = {
  typeId: null,
  typeName: null,
  typeStatus: null,
  createTime: null,
  updateTime: null,
  createby: null,
  updateby: null,
  showIndex: 1,
  typeIcon: null
}
export default {
  name: 'CourseType',
  components: { pagination, crudOperation, rrOperation, udOperation, uploadImage },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['discount_use'],
  cruds() {
    return CRUD({
      title: '课程分类',
      url: 'api/courseType',
      idField: 'typeId',
      sort: 'typeId,desc',
      crudMethod: { ...crudCourseType }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'courseType:add'],
        edit: ['admin', 'courseType:edit'],
        del: ['admin', 'courseType:del']
      },
      rules: {
        typeName: [{ required: true, message: '请输入课程分类名称', trigger: 'blur' }],
        typeStatus: [{ required: true, message: '请选择状态', trigger: 'change' }],
        typeIcon: [{ required: true, message: '请上传图标', trigger: 'change' }]
      },
      queryTypeOptions: [{ key: 'typeName', display_name: '名称' }]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    uploadAction(e) {
      this.form.typeIcon = e
    },
    doSubmitAction() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.$refs['uploadImage'].handleUpload().then((res) => {
            if (res) {
              this.crud.submitCU()
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped></style>
